package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.entity.PageResult;
import com.xinrui.sunnyday.entity.QueryPageBean;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.pojo.CheckGroup;
import com.xinrui.sunnyday.pojo.CheckItem;
import com.xinrui.sunnyday.service.CheckGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<CheckGroup> checkGroupPageInfo = checkGroupService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(checkGroupPageInfo.getTotal(), checkGroupPageInfo.getList());
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Integer id) {
        checkGroupService.delete(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询检查项
     *
     * @return
     */
    @GetMapping("/findAllCheckItem")
    public Result findAllCheckItem() {
        List<CheckItem> checkItemList = checkGroupService.findAllCheckItem();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }

    /**
     * 增加
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupService.add(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @PostMapping("/findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
     * 查询当前检查组的检查项
     */
    @PostMapping("/findCheckItemById")
    public Result findCheckItemById(Integer id) {
        Integer[] checkId = checkGroupService.findCheckItemById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkId);
    }

    /**
     * 编辑
     *
     * @param checkGroup
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup) {
        checkGroupService.update(checkGroup);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
     * 修改检查组中的检查项
     * @param groupId
     * @param itemId
     * @return
     */
    @PostMapping("/updateCheckItem")
    public Result updateCheckItem(Integer groupId, Integer[]itemId) {
        checkGroupService.updateCheckItem(groupId, itemId);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
}
